clear

u ${data}\bhpsjob,clear

cap drop _m

g year=doiy4
sort year
merge m:1 year using ${data}\urate_uk, keepusing(urate_uk urate_uk1 cpi)
drop if _m==2
drop _m
g lgu_uk=log(urate_uk)
g lgu_uk1=log(urate_uk1)

sort cjsbgy4
merge m:1 cjsbgy4 using ${data}\start_urate_uk
drop if _m==2
drop _m
g lgu_uk_start=log(start_urate_uk)

drop if region2<0

g female=sex-1
tab qual, g(educ)
g age2=age*age/100

*duration in current status
g dur1=idate-cjsbg_nd
g dur2=dur1*dur1/10
g dur3=dur2*dur1/100

*benefits
g hhben=100*fihhmb/cpi
g indben=100*fimnb/cpi
replace indben=0 if indben<0
replace hhben=0 if hhben<0
g houseben=f139>0
g lgindben=log(1+indben)

*savings
g s0=fiyrdi<0 if wave<=8
replace s0=fiyrdia<0 if wave>=9
g s1=fiyrdi==1 if wave<=8
replace s1=fiyrdia==0 if wave>=9
g s2=fiyrdi==2 if wave<=8
replace s2=fiyrdia>0 & fiyrdia<100 if wave>=9
g s3=fiyrdi>=3 & fiyrdi<=5 if wave<=8
replace s3=fiyrdia>=100 if wave>=9

*housing tenure
g h1=tenure==1
g h2=tenure==2
g h3=tenure==3
g h4=tenure>=4 & tenure<=8

*wages
replace lwg=lwg-log(cpi/100)
replace lrw=lrw-log(cpi/100)
g wage=exp(lwg)
g reswage=exp(lrw)

*jobs
g newjob=minjspno==1 & lwg<.
g job=wave if newjob==1
sort pid wave
quie by pid: replace job=job[_n-1] if job[_n-1]~=. & newjob==0
egen minjob0=min(wave) if newjob==1, by(pid)
egen minjob=mean(minjob0), by(pid)
replace job=0 if wave<minjob
replace job=0 if minjob==.

g year2=(year-1990)^2

egen cell=group(pid job)
drop if age<16 | age>65

***********
* TABLE 1 *
***********
global controls_w age age2 qqual2-qqual4 married nkids year year2 dur1-dur3 
*Column 1
reghdfe lwg lgu_uk $controls_w, a(region2 pid) cluster(year pid)
outreg2 using ${out}\Table1_UK.xls, drop($controls_w) replace nocons noaster paren se dec(3) excel
*Column 2
reghdfe lwg lgu_uk lgu_uk_start $controls_w, a(region2 pid) cluster(year pid)
outreg2 using ${out}\Table1_UK.xls, drop($controls_w) append nocons noaster paren se dec(3) excel
*Column 3
xtset cell
reghdfe lwg lgu_uk lgu_uk_start $controls_w, a(region2 cell) cluster(year cell)
outreg2 using ${out}\Table1_UK.xls, drop($controls_w) append nocons noaster paren se dec(3) excel
xtset pid
*Column 4
reghdfe lwg lgu_uk1 $controls_w, a(region2 pid) cluster(year pid)
outreg2 using ${out}\Table1_UK.xls, drop($controls_w) append nocons noaster paren se dec(3) excel

***********
* TABLE 2 *
***********
global controls_rw age age2 qqual2-qqual4 married nkids lgindben houseben year year2 dur1-dur3 
*Column 1
reghdfe lrw lgu_uk $controls_rw, a(region2 pid) cluster(year pid)
outreg2 using ${out}\Table2_UK.xls, drop($controls_rw) replace nocons noaster paren se dec(3) excel
*Column 2
reghdfe lrw lgu_uk1 $controls_rw, a(region2 pid) cluster(year pid)
outreg2 using ${out}\Table2_UK.xls, drop($controls_rw) append nocons noaster paren se dec(3) excel

